package concurrencyPractice.chapter02;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.math.BigInteger;

/**
 * @author xinya
 * @version 1.0
 * @project javacore
 * @description 程序2-6
 * @date 2023/12/21 18:04:19
 */
@ThreadSafe
public class SynchronizedFactorizer implements Servlet {
    @GuardeBy("this")
    private BigInteger lastNumber;
    @GuardeBy("this")
    private BigInteger[] lastFactors;

    @Override
    public synchronized void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        BigInteger i = extractFromRequest(servletRequest);
        if(i.equals(lastNumber)) {
            encodeIntoResponse(servletResponse, lastFactors);
        } else {
            BigInteger[] factors = factor[i];
            lastNumber = i;
            lastFactors = factors;
            encodeIntoResponse(servletResponse, factors);
        }

    }


    private void encodeIntoResponse(ServletResponse resp, BigInteger[] factors) {
    }

    private BigInteger extractFromRequest(ServletRequest req) {
        return null;
    }
}
